package cn.iocoder.yudao.module.scrm.controller.admin.schedules;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesTeacherPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesTeacherRespVO;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesTeacherSaveReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.schedulesteacher.SchedulesTeacherDO;
import cn.iocoder.yudao.module.scrm.service.schedules.SchedulesTeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 排期老师")
@RestController
@RequestMapping("/scrm/schedules-teacher")
@Validated
public class SchedulesTeacherController {

    @Resource
    private SchedulesTeacherService schedulesTeacherService;

    @PutMapping("/update")
    @Operation(summary = "更新排期老师")
    @PreAuthorize("@ss.hasPermission('scrm:schedules-teacher:update')")
    public CommonResult<Boolean> updateSchedulesTeacher(@Valid @RequestBody SchedulesTeacherSaveReqVO updateReqVO) {
        schedulesTeacherService.updateSchedulesTeacher(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除排期老师")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('scrm:schedules-teacher:delete')")
    public CommonResult<Boolean> deleteSchedulesTeacher(@RequestParam("id") Long id) {
        schedulesTeacherService.deleteSchedulesTeacher(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得排期老师")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('scrm:schedules-teacher:query')")
    public CommonResult<SchedulesTeacherRespVO> getSchedulesTeacher(@RequestParam("id") Long id) {
        SchedulesTeacherDO schedulesTeacher = schedulesTeacherService.getSchedulesTeacher(id);
        return success(BeanUtils.toBean(schedulesTeacher, SchedulesTeacherRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得排期老师分页")
    @PreAuthorize("@ss.hasPermission('scrm:schedules-teacher:query')")
    public CommonResult<PageResult<SchedulesTeacherRespVO>> getSchedulesTeacherPage(@Valid SchedulesTeacherPageReqVO pageReqVO) {
        PageResult<SchedulesTeacherDO> pageResult = schedulesTeacherService.getSchedulesTeacherPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SchedulesTeacherRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出排期老师 Excel")
    @PreAuthorize("@ss.hasPermission('scrm:schedules-teacher:export')")
    @OperateLog(type = EXPORT)
    public void exportSchedulesTeacherExcel(@Valid SchedulesTeacherPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SchedulesTeacherDO> list = schedulesTeacherService.getSchedulesTeacherPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "排期老师.xls", "数据", SchedulesTeacherRespVO.class,
                        BeanUtils.toBean(list, SchedulesTeacherRespVO.class));
    }

}